﻿

local moduleName = "RangeCheck"



----------------------------
--      Localization      --
----------------------------

local L = {
	[moduleName] = "Range Check",

	["Check frequency"] = true,
	["Frequency in seconds."] = true,

	["Set transparent"] = true,
	["Range check turns units which are out of range slightly transparent."] = true,
-- 40 yards range check
	["Lesser Heal"] = true, --PRIEST
	["Healing Touch"] = true, --DRUID
	["Holy Light"] = true, --PALADIN
	["Healing Wave"] = true, --SHAMAN
	["Misdirection"] = true, -- lv70 HUNTER 100 yards

}
if GetLocale() == "zhCN" then
	L[moduleName] = "距离检测"

	L["Check frequency"] = "更新频率"
	L["Frequency in seconds."] = "单位:秒"

	L["Set transparent"] = "设置透明度"
	L["Range check turns units which are out of range slightly transparent."] = "不在范围内的队友的透明度."

	L["Lesser Heal"] = "次级治疗术" --PRIEST
	L["Healing Touch"] = "治疗之触" --DRUID
	L["Holy Light"] = "圣光术" --PALADIN
	L["Healing Wave"] = "治疗波" --SHAMAN
	L["Misdirection"] = "误导" -- lv70 HUNTER 100 yards
elseif GetLocale() == "zhTW" then
	L[moduleName] = "距離檢測"

	L["Check frequency"] = "更新頻率"
	L["Frequency in seconds."] = "單位:秒"

	L["Set transparent"] = "設置透明度"
	L["Range check turns units which are out of range slightly transparent."] = "不在範圍內的隊友的透明度."

	L["Lesser Heal"] = "次級治療術" --PRIEST
	L["Healing Touch"] = "治療之觸" --DRUID
	L["Holy Light"] = "聖光術" --PALADIN
	L["Healing Wave"] = "治療波" --SHAMAN
	L["Misdirection"] = "誤導" -- lv70 HUNTER 100 yards

end

----------------------------------
--      Local Declaration      --
----------------------------------

local InRange


local IsSpellInRange = IsSpellInRange
local CheckInteractDistance = CheckInteractDistance



----------------------------------
--      Module Declaration      --
----------------------------------

local plugin = BunchOfBars:NewModule(moduleName, "AceTimer-3.0")

plugin.revision = tonumber(("$Revision: 40 $"):match("%d+"))

plugin.options = {
	name = L[moduleName],
	args = {
		frequency = {
			type = "range",
			name = L["Check frequency"],
			desc = L["Frequency in seconds."],
			min  = 0.1,
            		max  = 5.0,
            		step = 0.1,
            		get  = "GetSetFrequency",
            		set  = "GetSetFrequency",
		},
		transparent = {
			type = "range",
			name = L["Set transparent"],
			desc = L["Range check turns units which are out of range slightly transparent."],
			min  = 0.1,
            		max  = 1,
            		step = 0.1,
            		get  = "GetSetTransparent",
            		set  = "GetSetTransparent",
		}
	}
}

plugin.defaultDB = {
	profile = {
		frequency = 0.5,
		transparent = 0.5
	}
}

local class = select(2, UnitClass("player"))

	-- 40 yard range check
if class == "PRIEST" then
	InRange = function(unit) return IsSpellInRange(L["Lesser Heal"], unit) == 1 end 
elseif class == "DRUID" then
	InRange = function(unit) return IsSpellInRange(L["Healing Touch"], unit) == 1 end
elseif class == "PALADIN" then
	InRange = function(unit) return IsSpellInRange(L["Holy Light"], unit) == 1 end
elseif class == "SHAMAN" then
	InRange = function(unit) return IsSpellInRange(L["Healing Wave"], unit) == 1 end
elseif class == "HUNTER" and UnitLevel("player") == 70 then
	InRange = function(unit) return IsSpellInRange(L["Misdirection"], unit) == 1 end -- 100 yards
else
	InRange = function(unit) return CheckInteractDistance(unit, 1) end -- ~28 yards
end


----------------------------------
--      Module Functions        --
----------------------------------

function plugin:OnEnable()
	self:ScheduleRepeatingTimer(self.UpdateAll, self.db.profile.frequency, self)
end


function plugin:OnUpdate(frame)
if not UnitExists(frame.unit) then return end
	if InRange(frame.unit) then -- this will also hide dead and offline players
		frame:SetAlpha(1.0)
	elseif frame:GetAlpha() == 1.0 then
		frame:SetAlpha(self.db.profile.transparent)
	end
end



----------------------------------
--      Option Handlers         --
----------------------------------

function plugin:GetSetFrequency(_, v)
	if type(v) == "nil" then return self.db.profile.frequency end

	if self.db.profile.frequency ~= v then
		self.db.profile.frequency = v
		
		self:OnEnable()
	end
end

function plugin:GetSetTransparent(_, v)
	if type(v) == "nil" then return self.db.profile.transparent end

	if self.db.profile.transparent ~= v then
		self.db.profile.transparent = v
		
		self:UpdateAllWith(function(frame)
			frame:SetAlpha(self.db.profile.transparent)
		end)
		self:UpdateAll()
	end
end
